<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Banner extends Base
{
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $where = [
                'banner_type' => 1
            ];
            if(isset($keyword) && $keyword != ''){
                $where['banner_title'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($banner_where) && $banner_where != "")
            {
                $where['banner_where'] = $banner_where;
            }
            if(isset($banner_status) && $banner_status != "")
            {
                $where['banner_status'] = $banner_status;
            }
            if(isset($banner_more) && $banner_more != "")
            {
                $where['banner_more'] = $banner_more;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('banner b')
                ->join('up_pp pp', 'b.banner_pp_id = pp.pp_id', 'left')
                ->join('up_case c', 'b.banner_pp_id = c.case_id', 'left')
                ->join('up_store s', 'b.banner_s_id = s.s_id', 'left')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('banner b')
                ->join('up_pp pp', 'b.banner_pp_id = pp.pp_id', 'left')
                ->join('up_case c', 'b.banner_pp_id = c.case_id', 'left')
                ->join('up_store s', 'b.banner_s_id = s.s_id', 'left')
                ->join('up_u_user u', 'b.banner_pp_id = u.user_id', 'left')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('banner_status asc,banner_index desc,banner_sort desc,banner_update_time desc')
                ->field('b.*,pp_name,case_name,s_name,user_d_name')
                ->select();
            if($lists){
                foreach ($lists as $k => $v){
                    if($v['banner_where'] == 2){
                        $lists[$k]['pp_name'] = $v['case_name'];
                        $lists[$k]['s_name'] = '----';
                    }
                    if($v['banner_where'] == 3){
                        $lists[$k]['pp_name'] = $v['user_d_name'];
                        $lists[$k]['s_name'] = '----';
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'banner_type' => $banner_type,
                'banner_title' => $banner_title,
                'banner_img' => $banner_img,
                'banner_sort' => $banner_sort,
                'banner_status' => $banner_status,
                'banner_content' => $banner_content,
                'banner_pp_id' => $banner_pp_id,
                'banner_s_id' => $banner_s_id,
                'banner_time' => date('Y-m-d H:i:s'),
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            $error = '请上传图片！';
            if($banner_type == 2){
                $data['banner_img'] = $video;
                $error = '请上传视频！';
            }
            if(!$data['banner_img']){
                return json(['code' => 100, 'msg' => $error]);
            }
            $res = Db::table('up_banner')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $types = Db::table('up_types')
            ->field('t_id,t_name')
            ->order('t_sort desc')
            ->select();
        $pps = Db::table('up_pp')
            ->field('pp_id,pp_name')
            ->order('pp_sort desc')
            ->select();
        $this->assign('types',$types);
        $this->assign('pps',$pps);
        return $this->fetch('banner/banneradd');
    }
    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function bannerAdd2(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'banner_type' => $banner_type,
                'banner_title' => $banner_title,
                'banner_img' => $banner_img,
                'banner_sort' => $banner_sort,
                'banner_status' => $banner_status,
                'banner_content' => $banner_content,
                'banner_where' => $banner_where,
                'banner_time' => date('Y-m-d H:i:s'),
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            $error = '请上传图片！';
            if($banner_type == 2){
                $data['banner_img'] = $video;
                $error = '请上传视频！';
            }
            if(!$data['banner_img']){
                return json(['code' => 100, 'msg' => $error]);
            }
            $res = Db::table('up_banner')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $where = input('where');
        $this->assign('where',$where);
        return $this->fetch('banner/banneradd2');
    }
    /**
     * 添加case banner
     * @return mixed|\think\response\Json
     */
    public function bannerCaseAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'banner_title' => $banner_title,
                'banner_img' => $banner_img,
                'banner_sort' => $banner_sort,
                'banner_pp_id' => $banner_pp_id,
                'banner_status' => $banner_status,
                'banner_content' => $banner_content,
                'banner_where' => 2,
                'banner_time' => date('Y-m-d H:i:s'),
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            $error = '请上传图片！';
            if(!$data['banner_img']){
                return json(['code' => 100, 'msg' => $error]);
            }
            $res = Db::table('up_banner')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $pps = Db::table('up_case')
            ->where('case_status',1)
            ->where('case_done',2)
            ->field('case_id,case_name')
            ->order('case_sort desc')
            ->select();
        $this->assign('pps',$pps);
        return $this->fetch('banner/bannercaseadd');
    }
    /**
     * 添加设计师 banner
     * @return mixed|\think\response\Json
     */
    public function bannerUserAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'banner_title' => $banner_title,
                'banner_img' => $banner_img,
                'banner_sort' => $banner_sort,
                'banner_pp_id' => $banner_pp_id,
                'banner_status' => $banner_status,
                'banner_content' => $banner_content,
                'banner_where' => 3,
                'banner_time' => date('Y-m-d H:i:s'),
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            $error = '请上传图片！';
            if(!$data['banner_img']){
                return json(['code' => 100, 'msg' => $error]);
            }
            $res = Db::table('up_banner')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $pps = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_d_type',2)
            ->field('user_id,user_d_name,user_phone')
            ->select();
        $this->assign('pps',$pps);
        return $this->fetch('banner/banneruseradd');
    }
    
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $old = Db::table('up_banner')->where('banner_id', $banner_id)->find();
            $data = [
                'banner_title' => $banner_title,
                'banner_pp_id' => $banner_pp_id,
                'banner_s_id' => $banner_s_id,
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            if($old['banner_type'] == 1){
                $data['banner_img'] = $banner_img;
                $data['banner_content'] = $banner_content;
            }else{
                $data['banner_img'] = $video;
            }
            $res = Db::table('up_banner')->where('banner_id', $banner_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = Db::table('up_banner')
            ->where('banner_id', $banner_id)
            ->find();
        $typeid = Db::name('pp')
            ->where('pp_id',$banner['banner_pp_id'])
            ->value('pp_t_id');
        $types = Db::table('up_types')
            ->field('t_id,t_name')
            ->order('t_sort desc')
            ->select();
        $pps = Db::table('up_pp')
            ->where('pp_t_id',$typeid)
            ->field('pp_id,pp_name')
            ->order('pp_sort desc')
            ->select();
        $sss = Db::table('up_store')
            ->where('s_pp_id',$banner['banner_pp_id'])
            ->field('s_id,s_name')
            ->order('s_sort desc')
            ->select();
        $this->assign('types',$types);
        $this->assign('pps',$pps);
        $this->assign('sss',$sss);
        $this->assign('typeid',$typeid);
        $this->assign([
            'banner' => $banner
        ]);
        return $this->fetch('banner/banneredit');
    }
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit2(){
        if(Request::instance()->isPost()){
            extract(input());
            $old = Db::table('up_banner')->where('banner_id', $banner_id)->find();
            $data = [
                'banner_title' => $banner_title,
                'banner_where' => $banner_where,
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            if($old['banner_type'] == 1){
                $data['banner_img'] = $banner_img;
                $data['banner_content'] = $banner_content;
            }else{
                $data['banner_img'] = $video;
            }
            $res = Db::table('up_banner')->where('banner_id', $banner_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = Db::table('up_banner')
            ->where('banner_id', $banner_id)
            ->find();
        $this->assign([
            'banner' => $banner
        ]);
        return $this->fetch('banner/banneredit2');
    }
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerCaseEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'banner_title' => $banner_title,
                'banner_pp_id' => $banner_pp_id,
                'banner_img' => $banner_img,
                'banner_content' => $banner_content,
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_banner')->where('banner_id', $banner_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = Db::table('up_banner')
            ->where('banner_id', $banner_id)
            ->find();
        $this->assign([
            'banner' => $banner
        ]);
        $pps = Db::table('up_case')
            ->where('case_status',1)
            ->where('case_done',2)
            ->field('case_id,case_name')
            ->order('case_sort desc')
            ->select();
        $this->assign('pps',$pps);
        return $this->fetch('banner/bannercaseedit');
    }
    /**
     * 编辑设计师轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerUserEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'banner_title' => $banner_title,
                'banner_pp_id' => $banner_pp_id,
                'banner_img' => $banner_img,
                'banner_content' => $banner_content,
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_banner')->where('banner_id', $banner_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = Db::table('up_banner')
            ->where('banner_id', $banner_id)
            ->find();
        $this->assign([
            'banner' => $banner
        ]);
        $pps = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_d_type',2)
            ->field('user_id,user_d_name,user_phone')
            ->select();
        $this->assign('pps',$pps);
        return $this->fetch('banner/banneruseredit');
    }
    
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'banner_sort'=>input('value'),
        ];
        $res = Db::name('banner')->where('banner_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 100, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 100, 'msg' => '下架失败']);
        }
    }
    /**
     * 推荐
     * @return \think\response\Json
     */
    public function indexshow(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_index', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 取消推荐
     * @return \think\response\Json
     */
    public function indexhide(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_index', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function bannerDel(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 批量批量
     * @return mixed|\think\response\Json
     */
    public function ppll(){
        $id = input('id');
        if(!$id) return json(['code' => 100, 'msg' => '请选择数据']);
        $field = input('field');
        $value = input('value');
        $param = [
            $field=>$value
        ];
        if($value != 0){
            $where = [];
            if($value <= 5) $where['banner_where'] = ['<>',1];
            if($value == 6) $where['banner_where'] = ['<>',2];
            if($value == 7) $where['banner_where'] = ['<>',3];
            $have = Db::table('up_banner')->where($where)->find();
            if($have) return json(['code' => 100, 'msg' => '只能设置同类型下的']);
        }
        $res = Db::table('up_banner')->where('banner_id','in',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
}